package cn.xuexiluxian.open.admin.alioss;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class AliyunOssUtil {

    @Autowired
    private AliyunOssConfig aliyunOssConfig;
    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 上传
     * @param file
     * @return
     */
    public String uploadFile(File file, String filename){
        log.info("=>OSS文件上传开始："+file.getName());
        String endpoint = aliyunOssConfig.getEndpoint();
        String accessKeyId = aliyunOssConfig.getAccesskey();
        String accessKeySecret = aliyunOssConfig.getAccesssecret();
        String bucketName = aliyunOssConfig.getBucketname();
        String fileHost = aliyunOssConfig.getFilehost();
        System.out.println("endpoint: " + endpoint);

        if(null == file){
            return null;
        }

        OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
        try {
            //容器不存在，就创建
            if(!ossClient.doesBucketExist(bucketName)){
                ossClient.createBucket(bucketName);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }
            //创建文件路径
            String fileUrl = StrUtil.isBlank(fileHost) ? "" : (fileHost + "/") + filename;
            //上传文件
            PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileUrl, file));
            //设置权限 这里是公开读
            ossClient.setBucketAcl(bucketName,CannedAccessControlList.PublicRead);
            if(null != result){
                log.info("=>OSS文件上传成功,OSS地址：" + fileUrl);
                return endpoint + "/" + fileUrl;
            }
        }catch (OSSException oe){
            log.error(oe.getMessage());
        }catch (ClientException ce){
            log.error(ce.getMessage());
        }finally {
            //关闭
            ossClient.shutdown();
        }
        return null;
    }


    /**
     * 删除
     * @param fileKey
     * @return
     */
    public String deleteFile(String fileKey){
        log.info("=>OSS文件删除开始");
        String endpoint = aliyunOssConfig.getEndpoint();
        String accessKeyId = aliyunOssConfig.getAccesskey();
        String accessKeySecret = aliyunOssConfig.getAccesssecret();
        String bucketName = aliyunOssConfig.getBucketname();
        String fileHost = aliyunOssConfig.getFilehost();
        try {
            OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
            if(!ossClient.doesBucketExist(bucketName)){
                log.info("=>您的Bucket不存在");
                return "您的Bucket不存在";
            }else {
                log.info("=>开始删除Object");
                ossClient.deleteObject(bucketName,fileKey);
                log.info("=>Object删除成功："+fileKey);
                return "=>Object删除成功："+fileKey;
            }
        }catch (Exception ex){
            log.info("删除Object失败",ex);
            return "删除Object失败";
        }
    }

    /**
     * 删除
     * @param fileKey
     * @return
     */
    public InputStream downloadFile(String fileKey){
        log.info("=========>OSS文件下载开始");
        String endpoint = aliyunOssConfig.getEndpoint();
        String accessKeyId = aliyunOssConfig.getAccesskey();
        String accessKeySecret = aliyunOssConfig.getAccesssecret();
        String bucketName = aliyunOssConfig.getBucketname();
        String fileHost = aliyunOssConfig.getFilehost();
        try {
            OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
            if(!ossClient.doesBucketExist(bucketName)){
                log.info("==============>您的Bucket不存在");
                return null;
            }else {
                log.info("==============>开始下载Object");
                OSSObject object = ossClient.getObject(bucketName, fileHost + fileKey);
                log.info("==============>Object下载成功："+fileKey);
                return object.getObjectContent();
            }
        }catch (Exception ex){
            log.info("下载Object失败",ex);
            return null;
        }
    }

    /**
     * 查询文件名列表
     * @param bucketName
     * @return
     */
    public List<String> getObjectList(String bucketName, String fileDir){
        List<String> listRe = new ArrayList<>();
        String endpoint = aliyunOssConfig.getEndpoint();
        String accessKeyId = aliyunOssConfig.getAccesskey();
        String accessKeySecret = aliyunOssConfig.getAccesssecret();
        try {
            log.info("=>查询文件名列表");
            OSSClient ossClient = new OSSClient(endpoint,accessKeyId,accessKeySecret);
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
            //列出11111目录下今天所有文件
            listObjectsRequest.setPrefix(fileDir + format.format(new Date()) + "/");
            ObjectListing list = ossClient.listObjects(listObjectsRequest);
            for(OSSObjectSummary objectSummary : list.getObjectSummaries()){
                System.out.println(objectSummary.getKey());
                listRe.add(objectSummary.getKey());
            }
            return listRe;
        }catch (Exception ex){
            log.info("=>查询列表失败",ex);
            return new ArrayList<>();
        }
    }

}
